package com.gitee.mazhenggg.demo.log;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.util.Properties;

public class LogApplication {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> data = env.readTextFile("C:/codes/demo/demo-log/src/main/resources/access.log").setParallelism(1);
        DataStream<Tuple2<String, Integer>> wordCounts = data
                .flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public void flatMap(String value, Collector<Tuple2<String, Integer>> out) {
                        for (String word : value.split("\\s")) {
                            out.collect(Tuple2.of(word, 1));
                        }
                    }
                });
        DataStream<Tuple2<String, Integer>> windowCounts = wordCounts
                .keyBy(0)
                .timeWindow(Time.seconds(5))
                .sum(1);
        windowCounts.print().setParallelism(1);
        env.execute("Socket Window WordCount");
        //SingleOutputStreamOperator<Nginxlog> nginxlog = data.map(new MapNginxLog());
        //nginxlog.addSink(new FlinkKafkaProducer<Nginxlog>("192.168.0.133:9092", "nginxlog-product", new NginxLogSchema())).setParallelism(1);
        env.execute("nginx日志处理！");
    }
}
